package com.iqiyi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iqiyi.dto.ActionResult;
import com.iqiyi.dto.VideoList;
import com.iqiyi.service.SearchService;
import com.iqiyi.util.QiniuUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RequestMapping("/search")
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;
    @RequestMapping(value = "/findVideos",method = RequestMethod.GET)
    public ActionResult findVideo(String name){
        List all =  searchService.findVideo(name);
        if (all.size()>0){
            return new ActionResult(200,"搜索视频成功",all);
        }else{
            List actor = searchService.findActor(name);
            if(actor.size()>0){
                return new ActionResult(201,"搜索演员成功",actor);
            }
        }
        return new ActionResult(403,"查询失败",null);
    }
    @RequestMapping("/findAllVideo")
    public ActionResult findAllVideo(Integer currentPage,Integer pageSize,String vname){
        PageHelper.startPage(currentPage,pageSize);
        List<VideoList> videos = searchService.findAllVideo(vname);
        PageInfo<VideoList> videoPageInfo = new PageInfo<>(videos);
        return new ActionResult(200,"ok",videoPageInfo);
    }
    @RequestMapping(value = "/uploadUp",method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file){
        String name = UUID.randomUUID().toString();
        try {
            String url = QiniuUpload.updateFile(file,name);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @RequestMapping(value = "/updateVideo",method = RequestMethod.PUT)
    public ActionResult updateVideo(@RequestBody VideoList videoList){
        Boolean aBoolean = searchService.updateVideo(videoList);
        if(aBoolean!=null){
            return new ActionResult(200,"update ok",null);
        }
        return new ActionResult(403,"update faild",null);
    }

}
